package com.zjq.tips
{
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	import flash.geom.Point;
	
	import mx.containers.Canvas;
	import mx.controls.TextInput;
	
	/**
	 * 实现带有线的提示的类
	 * **/
	
	public class TipWithLine extends Canvas
	{
		private var _tiTip:TextInput = new TextInput();  //提示信息
		private var _lineEndPoint:Point;
		private var _currLineStartP:Point = new Point;
		private var _currLineEndP:Point = new Point;
		
		public function TipWithLine(lineEndPoint:Point)
		{
			super();
			this.width = 200;
			this.height = 300;
			this.addChild(_tiTip);
			_tiTip.addEventListener(MouseEvent.ROLL_OVER, tipRollOverHandler);
			_tiTip.addEventListener(MouseEvent.ROLL_OUT, tipRollOutHandler);
			_lineEndPoint = lineEndPoint;
		}
		/**
		 * 设置文本内容
		*/
		public function set tipTxt(str:String):void
		{			
			_tiTip.editable = false;
			_tiTip.text = str;
			_tiTip.validateNow();
			_tiTip.width = _tiTip.textWidth+8;
			_tiTip.height = _tiTip.textHeight +6;
		}
		
		/**
		 * 设置位置，重新画连线
		 * @param pStartPoint为连线的其中一端，另一端为_lineEndPoint*/
		public function resetPosition(pStartPoint:Point):void
		{
			var lineStartP:Point = new Point(0, 0);
			var lineEndP:Point = new Point(0, 0);
			if (pStartPoint.x < _lineEndPoint.x)
			{
				if (pStartPoint.y < _lineEndPoint.y)  
				{					
					this.x = pStartPoint.x - _tiTip.width;
					this.y = pStartPoint.y - _tiTip.height*0.5;
					_tiTip.x = 0;
					_tiTip.y = 0;
					lineStartP.x  = _tiTip.width;
					lineStartP.y = _tiTip.height*0.5;
					lineEndP.x  = _tiTip.width + Math.abs(pStartPoint.x - _lineEndPoint.x);
					lineEndP.y = _lineEndPoint.y - pStartPoint.y + _tiTip.height*0.5;
					this.width = Math.abs(pStartPoint.x - _lineEndPoint.x) + _tiTip.width;
					this.height =Math.abs(pStartPoint.y - _lineEndPoint.y) + _tiTip.height;
				}else {	 //if (pStartPoint.y >= _lineEndPoint.y)			
					this.x = pStartPoint.x - _tiTip.width;
					this.y = _lineEndPoint.y;
					_tiTip.x = 0;
					_tiTip.y = Math.abs(pStartPoint.y - _lineEndPoint.y) - _tiTip.height*0.5;
					lineStartP.x  = _tiTip.width;
					lineStartP.y = Math.abs(pStartPoint.y - _lineEndPoint.y);
					lineEndP.x  = _tiTip.width + Math.abs(pStartPoint.x - _lineEndPoint.x);
					lineEndP.y = 0;
					this.width = Math.abs(pStartPoint.x - _lineEndPoint.x) + _tiTip.width;
					this.height =Math.abs(pStartPoint.y - _lineEndPoint.y) + _tiTip.height*0.5;
				}
			}else {  //if (pStartPoint.x >= _lineEndPoint.x)				
				if (pStartPoint.y < _lineEndPoint.y)  
				{					
					this.x = _lineEndPoint.x;
					this.y = pStartPoint.y - _tiTip.height*0.5;
					_tiTip.x = pStartPoint.x - _lineEndPoint.x;
					_tiTip.y = 0;
					lineStartP.x  = pStartPoint.x - _lineEndPoint.x;
					lineStartP.y = _tiTip.height*0.5;
					lineEndP.x  = 0;
					lineEndP.y = _lineEndPoint.y - pStartPoint.y + _tiTip.height*0.5;
					this.width = Math.abs(pStartPoint.x - _lineEndPoint.x) + _tiTip.width;
					this.height =Math.abs(pStartPoint.y - _lineEndPoint.y) + _tiTip.height;
				}else {	 //if (pStartPoint.y >= _lineEndPoint.y)				
					this.x = _lineEndPoint.x;
					this.y = _lineEndPoint.y;
					_tiTip.x = pStartPoint.x - _lineEndPoint.x;
					_tiTip.y = pStartPoint.y - _lineEndPoint.y - _tiTip.height*0.5;
					lineStartP.x  = pStartPoint.x - _lineEndPoint.x;
					lineStartP.y = pStartPoint.y - _lineEndPoint.y;
					lineEndP.x  = 0;
					lineEndP.y = 0;
					this.width = Math.abs(pStartPoint.x - _lineEndPoint.x) + _tiTip.width;
					this.height =Math.abs(pStartPoint.y - _lineEndPoint.y) + _tiTip.height;
				}
			}
			drawLine(lineStartP, lineEndP);
		}
		
		/**
		 * 画连线**/
		private function drawLine(pStartPoint:Point, pEndPoint:Point, pColor:uint = 0x333333):void
		{
			this.graphics.clear();
			this.graphics.lineStyle(1, pColor);
			this.graphics.moveTo(pStartPoint.x, pStartPoint.y);
			this.graphics.lineTo(pEndPoint.x, pEndPoint.y);		
			_currLineStartP.x = pStartPoint.x;
			_currLineStartP.y = pStartPoint.y;
			_currLineEndP.x = pEndPoint.x;
			_currLineEndP.y = pEndPoint.y;
		}
		
		/**
		 *鼠标划过文本框，线条颜色变化
		 *  **/
		private function tipRollOverHandler(event:MouseEvent):void
		{			
			drawLine(_currLineStartP, _currLineEndP, 0x33ff33);	
		}
		
		/**
		 *鼠标移出文本框，线条颜色恢复
		 *  **/
		private function tipRollOutHandler(event:MouseEvent):void
		{			
			drawLine(_currLineStartP, _currLineEndP, 0x333333);		
		}
		
	}
}